package io.geekidea.boot.test.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.geekidea.boot.framework.exception.BusinessException;
import io.geekidea.boot.framework.page.OrderByItem;
import io.geekidea.boot.framework.page.OrderMapping;
import io.geekidea.boot.framework.page.Paging;
import io.geekidea.boot.test.dto.InvoiceRecordDto;
import io.geekidea.boot.test.entity.InvoiceRecord;
import io.geekidea.boot.test.mapper.InvoiceRecordMapper;
import io.geekidea.boot.test.query.InvoiceRecordQuery;
import io.geekidea.boot.test.service.InvoiceRecordService;
import io.geekidea.boot.test.vo.InvoiceRecordVo;
import io.geekidea.boot.test.query.AppInvoiceRecordQuery;
import io.geekidea.boot.test.vo.AppInvoiceRecordVo;
import io.geekidea.boot.util.PagingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

/**
 * 发票申请记录 服务实现类
 *
 * @author geekidea
 * @since 2025-03-26
 */
@Slf4j
@Service
public class InvoiceRecordServiceImpl extends ServiceImpl<InvoiceRecordMapper, InvoiceRecord> implements InvoiceRecordService {

    @Autowired
    private InvoiceRecordMapper invoiceRecordMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addInvoiceRecord(InvoiceRecordDto dto) {
        InvoiceRecord invoiceRecord = new InvoiceRecord();
        BeanUtils.copyProperties(dto, invoiceRecord);
        return save(invoiceRecord);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateInvoiceRecord(InvoiceRecordDto dto) {
        Long id = dto.getId();
        InvoiceRecord invoiceRecord = getById(id);
        if (invoiceRecord == null) {
            throw new BusinessException("发票申请记录不存在");
        }
        BeanUtils.copyProperties(dto, invoiceRecord);
        return updateById(invoiceRecord);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteInvoiceRecord(Long id) {
        return removeById(id);
    }

    @Override
    public InvoiceRecordVo getInvoiceRecordById(Long id) {
        return invoiceRecordMapper.getInvoiceRecordById(id);
    }

    @Override
    public Paging<InvoiceRecordVo> getInvoiceRecordPage(InvoiceRecordQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<InvoiceRecordVo> list = invoiceRecordMapper.getInvoiceRecordPage(query);
        Paging<InvoiceRecordVo> paging = new Paging<>(list);
        return paging;
    }

    @Override
    public AppInvoiceRecordVo getAppInvoiceRecordById(Long id) {
        return invoiceRecordMapper.getAppInvoiceRecordById(id);
    }

    @Override
    public Paging<AppInvoiceRecordVo> getAppInvoiceRecordPage(AppInvoiceRecordQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<AppInvoiceRecordVo> list = invoiceRecordMapper.getAppInvoiceRecordPage(query);
        Paging<AppInvoiceRecordVo> paging = new Paging<>(list);
        return paging;
    }

}
